package com.oocl.euc.core;

import com.oocl.euc.framework.common.email.EmailEntity;
import org.hamcrest.Description;
import org.mockito.ArgumentMatcher;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class IsEmailEntity<T> extends ArgumentMatcher<T> {

    private EmailEntity emailEntity;
    private String validationResult;

    public IsEmailEntity(EmailEntity emailEntity) {
        this.emailEntity = emailEntity;
    }

    @Override
    public boolean matches(Object o) {
        if (!(o instanceof EmailEntity)) {
            validationResult = "class not match";
            return false;
        }
        EmailEntity emailEntity1 = (EmailEntity)o;
        if(!eqAttachment(emailEntity1)){
            return false;
        }

        return Arrays.equals(emailEntity1.getBcc(),emailEntity.getBcc())&&
                Arrays.equals(emailEntity1.getCc(),emailEntity.getCc()) &&
               emailEntity1.getContent().equals(emailEntity.getContent()) &&
               emailEntity1.getSubject().equals(emailEntity.getSubject()) &&
                Arrays.equals(emailEntity1.getTo(),emailEntity.getTo()) &&
               emailEntity1.getFrom().equals(emailEntity.getFrom());
    }

    private boolean eqAttachment(EmailEntity emailEntity1) {

        Map<String,File> map1 = new HashMap<String, File>();
        Map<String,File> map = new HashMap<String, File>();
        if (emailEntity1.getAttachmentFiles()!=null){
             map1 = emailEntity1.getAttachmentFiles();
        }
        if (emailEntity.getAttachmentFiles()!=null){
             map = emailEntity.getAttachmentFiles();
        }
        return map.equals(map1);

    }

    public void describeTo(Description description) {
        super.describeTo(description);
        description.appendText("\n" + validationResult);
    }


}
